﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Computer_Manager.DTO;
using Computer_Manager.BUS;
using System.Security.Cryptography;

namespace Computer_Manager.GUI
{
    public partial class frmLogin : DevExpress.XtraEditors.XtraForm
    {
        public frmLogin()
        {
            InitializeComponent();
        }
        BUS_user bus_user = new BUS_user();
        DTO_user dto_user = null;

        private void btnLogin_Click(object sender, EventArgs e)
        {
            dto_user = new DTO_user();
            dto_user.username = txtUsername.Text.Trim();
            dto_user.password = MD5(txtPassword.Text);
            var dt = bus_user.CheckLogin(dto_user);
            if (dt.Rows.Count>0)
            {
                XtraMessageBox.Show("Đăng nhập thành công.","Thông tin",MessageBoxButtons.OK,MessageBoxIcon.Information);
                this.Hide();
                frmMain frmMain = new frmMain();
                frmMain.User = dt;
                frmMain.Show();
            }
            else
            {
                XtraMessageBox.Show("Đăng nhập không thành công. Vui lòng kiểm tra lại", "Thông tin", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        //Hàm mã hoá MD5
        public static string MD5(string x)
        {
            MD5 md5 = new MD5CryptoServiceProvider();

            //compute hash from the bytes of text
            md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(x));

            //get hash result after compute it
            byte[] result = md5.Hash;

            StringBuilder strBuilder = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                //change it into 2 hexadecimal digits
                //for each byte
                strBuilder.Append(result[i].ToString("x2"));
            }

            return strBuilder.ToString();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}